﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using Senparc.Weixin.MP.Entities;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

public class SMZDMOP
{
    static string sperContentBox = "<!--s perContentBox -->";
    static string perContenBox = "<div class=\"perContentBox\" id=\"";
    public static List<Article> ParsingIndex()
    {
        string[] perContentBoxs = SplitStr(GetHtml(), sperContentBox);
         return GetDiv(perContentBoxs);
    }
    public static List<Article> ReadRss()
    {
        List<Article> articles = new List<Article>();
        var xdoc = XDocument.Load("http://feed.smzdm.com",LoadOptions.PreserveWhitespace);
        XNamespace content = "http://purl.org/rss/1.0/modules/content/";
        var feeds = from item in xdoc.Root.Element("channel").Elements("item").Take(5)
                    select new Feed() {
                        Title = item.Element("title").Value,
                        Link = item.Element("link").Value,
                        Description = item.Element("description").Value,
                        PubDate = item.Element("pubDate").Value  ,
                        Img = GetImgUrl(item.Element(content.GetName("encoded")).Value)
                    }   ;
        foreach (var s in feeds)
        {
            Article article = new Article();
            article.Title = s.Title.ToString();
            article.Url = s.Link;
            article.Description = s.Description.Replace("<![CDATA[<p>", "").Replace("<p>","").Substring(0, 50);
            article.PicUrl = s.Img;
            articles.Add(article);
        }         
        //<![CDATA[<p>


        //if (feed.CanRead(rssReader))
        //{
        //    feed.ReadFrom(rssReader);
        // }
          
        //foreach (var item in feed.Feed.Items)
        //{
        //    Article article = new Article();
        //    article.Title = item.Title.ToString();
        //    article.Url = item.Links.ToString();
        //    article.Description = item.Summary.ToString();
        //    articles.Add(article);
        //}

        return articles;
    }
    public static string GetImgUrl(string str) {
        string imgstr="<img src=\"";
        imgstr = str.Substring(0, str.IndexOf(imgstr) + imgstr.Length);
       str= str.Replace(imgstr, "");
       str = str.Split('"')[0];
    return str;
    }
    public static string GetHtml()
    {
        string rl;
        WebRequest Request = WebRequest.Create("http://www.smzdm.com/");
        WebResponse Response = Request.GetResponse();
        Stream resStream = Response.GetResponseStream();
        StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
        StringBuilder sb = new StringBuilder();
        while ((rl = sr.ReadLine()) != null)
        {
            sb.Append(rl);
        }
        return sb.ToString();
    }
    #region 分解字符串为数组
    /// <summary>
    /// 字符串分函数
    /// </summary>
    /// <param name="str">要分解的字符串</param>
    /// <param name="splitstr">分割符,可以为string类型</param>
    /// <returns>字符数组</returns>
    public static string[] SplitStr(string str, string splitstr)
    {
        if (splitstr != "")
        {
            System.Collections.ArrayList c = new System.Collections.ArrayList();
            while (true)
            {
                int thissplitindex = str.IndexOf(splitstr);
                if (thissplitindex >= 0)
                {
                    c.Add(str.Substring(0, thissplitindex));
                    str = str.Substring(thissplitindex + splitstr.Length);
                }
                else
                {
                    c.Add(str);
                    break;
                }
            }
            string[] d = new string[c.Count];
            for (int i = 0; i < c.Count; i++)
            {
                d[i] = c[i].ToString();
            }
            return d;
        }
        else
        {
            return new string[] { str };
        }
    }
    #endregion

    #region 分解div内容
    public static List<Article> GetDiv(string[] divs)
    {
        string astr = "<a href=\"";
        string descstr="<p class=\"p_excerpt\">";
        string imgstr = "class=\"imgBox\"><img src=\"";
        string tempFront = string.Empty;
        List<Article> articles = new List<Article>();
        int i = 0;
        foreach (string div in divs)
        {
            if (div.Contains(perContenBox) && i < 3 )
            {
                Article article = new Article();
                //去掉 a标签前面内容
                tempFront = div.Substring(0, div.IndexOf(astr) + astr.Length);
                string tempDiv = div.Replace(tempFront, "");
                article.Url = tempDiv.Split('"')[0];
                //去掉url内容 到title
                tempDiv = tempDiv.Remove(0, article.Url.Length + "\" title=\"".Length);
                article.Title = tempDiv.Split('"')[0];
                //去掉内容到img
                tempFront = tempDiv.Substring(0, tempDiv.IndexOf(imgstr) + imgstr.Length);
                tempDiv = tempDiv.Replace(tempFront, "");
                article.PicUrl = tempDiv.Split('"')[0];
                //描述
                tempFront=tempDiv.Substring(0,tempDiv.IndexOf(descstr)+descstr.Length);
                tempDiv=tempDiv.Replace(tempFront,"");
                article.Description=tempDiv.Split('<')[0];
                 
                articles.Add(article);
                i++;
            }
        }
        return articles;
    }


    #endregion
}
public class Feed {
    public string Title { get; set; }

    public string Link { get; set; }

   public string PubDate{get;set;}
   public string Description{get;set;}
    public string Img{get;set;}

   
}

